﻿using Book.Entities;
using Book.IRepositories;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Http.Filters;

namespace Book.Controllers
{
    [Route("api/member")]
    [ApiController]
    [EnableCors("any")]
    public class MemberController : ControllerBase
    {
        private readonly ILabMemberRepository _labMemberRepository;

        public MemberController(ILabMemberRepository labMenberRepository)
        {
            this._labMemberRepository = labMenberRepository;
        }
        [HttpPost("addmember")]
        public IActionResult AddMember()
        {      
            string name = Request.Form["name"];
            string yjfx = Request.Form["yjfx"];
            string identity = Request.Form["identity"];
            string photo = Request.Form["photo"];
            string title = Request.Form["title"];
            double order = double.Parse(Request.Form["order"]);
            string phone = Request.Form["phone"];
            string address = Request.Form["address"];
            string email = Request.Form["email"];
            string photoname = Request.Form["photoname"];
            var res=_labMemberRepository.AddMember(name, yjfx, identity, photo, photoname, title,order, phone, address,  email);
            return Ok(res);
        }

        [HttpGet("getallmember")]
        public IActionResult GetAllMemeber()
        {
            var rel = _labMemberRepository.GetAllMember();
            return Ok(rel);
        }
        [HttpPost("updatemember")]
        public IActionResult UpdateMember()
        {
            string id = Request.Form["id"];
            string name = Request.Form["name"];
            string yjfx = Request.Form["yjfx"];
            string identity = Request.Form["identity"];
            string photo = Request.Form["photo"];
            string title = Request.Form["title"];
            double order = double.Parse(Request.Form["order"]);
            string phone = Request.Form["phone"];
            string address = Request.Form["address"];
            string email = Request.Form["email"];
            string photoname = Request.Form["photoname"];
            _labMemberRepository.UpdateMember(id,name, yjfx, identity, photo, photoname, title, order, phone, address, email);
            return Ok();
        }
        [HttpPost("updateppage")]
        public IActionResult UpdatePPage([FromBody] JObject parameter)
        {
            string id = parameter.Value<string>("id");
            string value = parameter.Value<string>("value");
            string content = parameter.Value<string>("content");
            string contentmd = parameter.Value<string>("contentmd");
            _labMemberRepository.UpdatePPage(id,  value, content,contentmd);
            return Ok();
        }
        [HttpPost("deletemember")]
        public IActionResult DeleteMember([FromBody] JObject parameter)
        {
            string id = parameter.Value<string>("id");
            _labMemberRepository.DeleteMember(id);
            return Ok();
        }
        [HttpPost("getmemberbyidentity")]
        public IActionResult GetMemberByIdentity([FromBody] JObject parameter)
        {
            string identity = parameter.Value<string>("identity");
            var res=_labMemberRepository.GetMemberByIdentity(identity);
            return Ok(res);
        }
        [HttpPost("getmemberbyid")]
        public IActionResult GetMemberById([FromBody] JObject parameter)
        {
            string id = parameter.Value<string>("id");
            var res = _labMemberRepository.GetMemberById(id);
            return Ok(res);
        }
    }
}
